package com.base.cn.platform.os.interceptor;

import com.base.cn.platform.os.common.constants.CommonConstants;
import com.base.cn.platform.os.common.utils.web.DeviceUtils;
import com.base.cn.platform.os.common.utils.web.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 访问路径拦截器，如果使用手机浏览器访问时自动路径H5站
 * @author s.li
 * @create 2018-10-16-14:38
 */
public class WebAccessJumpInterceptor implements HandlerInterceptor {
    private static List<String> uriList = new ArrayList<>();
    static {
        uriList.add("/web/index");
        uriList.add("/web/course/courseList");
        uriList.add("/web/course/liveIndex");
        uriList.add("/web/exam/index/examIndex");
        uriList.add("/web/teacher/list");
        uriList.add("/web/classes/index");
        uriList.add("/web/qa/listQuestion");
        uriList.add("/web/article/articleIndex");
        uriList.add("/web/book/list");
        uriList.add("/web/course/open/index");
    }
    @Autowired
    private CommonConstants commonConstants;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        boolean isMobile = DeviceUtils.isMobileDevice(request);
        String requestURI = request.getRequestURI();
        if(isMobile){
            StringBuilder mobilePath = new StringBuilder(commonConstants.get(CommonConstants.MOBILE_PATH));
            //检查链接是否来自手机端
            String params = WebUtils.getRequestParameter(request);
            switch (requestURI){
                case "/web/course/courseInfo":
                    mobilePath.append("/mobile/course/info?").append(params);
                    break;
                case "/web/classes/info":

                    mobilePath.append("/mobile/class/info?").append(params.replace("id","classId"));
                    break;
                case "/web/classes/topic/info":
                    mobilePath.append("/mobile/class/topic/info?").append(params);
                    break;

                case "/web/course/liveInfo":
                    mobilePath.append("/mobile/course/liveInfo?").append(params);
                    break;
                case "/web/article/articleInfoById":
                    mobilePath.append("/mobile/article/articleInfoById?").append(params);
                    break;
            }
            response.sendRedirect(mobilePath.toString());
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
